﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraLayout.Utils;
using DevExpress.XtraLayout;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraTreeList;

namespace SAF.Controls
{
    [ToolboxItem(false)]
    [EditorBrowsable(EditorBrowsableState.Never)]
    public partial class TreeLookUpEditPopup : DevExpress.XtraEditors.XtraUserControl
    {
        RepositoryItemTreeLookUpEdit owner;
        TreeList tree;

        public TreeLookUpEditPopup()
        {
            InitializeComponent();
        }

        internal TreeLookUpEditPopup(RepositoryItemTreeLookUpEdit owner)
            : this()
        {
            this.owner = owner;
        }

        protected internal virtual void BeforeShowing()
        {
            lciClear.Visibility = Owner.ShowClearButton ? LayoutVisibility.Always : LayoutVisibility.Never;
        }
        protected bool CheckHasVisibleItems(LayoutControlGroup group)
        {
            foreach (BaseLayoutItem item in group.Items)
            {
                LayoutControlItem controlItem = item as LayoutControlItem;
                if (controlItem == null) continue;
                if (controlItem.Visibility != LayoutVisibility.Never && controlItem.Control != null && !(controlItem.Control is LabelControl)) return true;
            }
            return false;
        }
        [Browsable(false)]
        public bool IsFocused
        {
            get
            {
                if (ContainsFocus) return true;
                return Tree != null && Tree.Focused;
            }
        }

        protected TreeLookUpEdit OwnerEdit { get { return Owner == null ? null : Owner.OwnerEdit; } }
        protected RepositoryItemTreeLookUpEdit Owner { get { return owner; } }

        internal void SetupTree(TreeList tree)
        {
            this.tree = tree;

            this.tree.DoubleClick += new EventHandler(tree_DoubleClick);

            this.tree.OptionsView.ShowFocusedFrame = false;
            this.tree.OptionsView.ShowIndicator = false;
            this.tree.OptionsView.ShowHorzLines = false;
            this.tree.OptionsView.ShowVertLines = false;

            this.tree.OptionsSelection.EnableAppearanceFocusedCell = false;

            this.tree.OptionsPrint.PrintHorzLines = false;
            this.tree.OptionsPrint.PrintVertLines = false;
            this.tree.OptionsPrint.UsePrintStyles = true;

            this.tree.OptionsMenu.EnableFooterMenu = false;

            this.tree.OptionsBehavior.AllowExpandOnDblClick = false;
            this.tree.OptionsBehavior.AutoChangeParent = false;
            this.tree.OptionsBehavior.AutoNodeHeight = false;
            this.tree.OptionsBehavior.AutoSelectAllInEditor = false;
            this.tree.OptionsBehavior.CloseEditorOnLostFocus = false;
            this.tree.OptionsBehavior.Editable = false;
            this.tree.OptionsBehavior.KeepSelectedOnClick = false;
            this.tree.OptionsBehavior.ResizeNodes = false;
            this.tree.OptionsBehavior.SmartMouseHover = false;

            Controls.Add(tree);
            lciGrid.BeginInit();
            Control prev = lciGrid.Control;
            lciGrid.Control = null;
            lciGrid.Control = tree;
            lciGrid.ContentVisible = true;
            prev.Dispose();
            lciGrid.EndInit();
        }

        void tree_DoubleClick(object sender, EventArgs e)
        {
            OwnerForm.ClosePopup();
        }

        public Control Find(string name)
        {
            Control[] res = Controls.Find(name, true);
            if (res != null && res.Length > 0) return res[0];
            return null;
        }

        protected PopupTreeLookUpEditForm OwnerForm
        {
            get
            {
                if (Owner == null || Owner.OwnerEdit == null || Owner.OwnerEdit == null) return null;
                return Owner.OwnerEdit.PopupFormCore as PopupTreeLookUpEditForm;
            }
        }

        protected TreeList Tree
        {
            get { return tree; }
        }

        internal bool IsShouldCloseOnEnter()
        {
            if (!Tree.Focused) return false;
            return false;
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            if (OwnerForm != null) OwnerForm.ClearClick();
        }
    }
}
